﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PK3Model;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace OneLine
{
    class Wave
    {
        private int numOfEnemies; // Number of enemies to spawn
        private int waveNumber; // What wave is this?

        private float spawnTimer = 0; // When should we spawn an enemy
        private int enemiesSpawned = 0; // How mant enemies have spawned

        private bool enemyAtEnd; // Has an enemy reached the end of the path?
        private bool spawningEnemies; // Are we still spawing enemies?

        private Level level; // A reference of the level

        private CPK3Model enemyModel; // A texture for the enemies
        public List<Enemy> enemies = new List<Enemy>(); // List of enemies

        private Pathfinder pathfinder;

        public bool RoundOver
        {
            get
            {
                return enemies.Count == 0 && enemiesSpawned == numOfEnemies;
            }
        }
        public int RoundNumber
        {
            get { return waveNumber; }
        }

        public bool EnemyAtEnd
        {
            get { return enemyAtEnd; }
            set { enemyAtEnd = value; }
        }
        public List<Enemy> Enemies
        {
            get { return enemies; }
        }

        public Wave(int waveNumber, int numOfEnemies, Pathfinder pathfinder, Level level, CPK3Model enemyModel)
        {
            this.waveNumber = waveNumber;
            this.numOfEnemies = numOfEnemies;

            this.level = level;
            this.enemyModel = enemyModel;
            this.pathfinder = pathfinder;
        }

        private void AddEnemy()
        {
            Enemy enemy = new Enemy(enemyModel)
            {
                Position = new Vector3(0.5f, 0.0f, 0.5f)
            };

            //if (waveNumber == 5)
            //{
            //    float speed = 2.0f;

            //    enemy = new Enemy(enemyTexture,
            //        level.Waypoints.Peek(), 50, 1, speed);
            //}

            enemies.Add(enemy);

            spawnTimer = 0;
            enemiesSpawned++;
        }

        public void Start()
        {
            spawningEnemies = true;
        }

        public void Update(GameTime gameTime)
        {
            if (enemiesSpawned == numOfEnemies)
                spawningEnemies = false; // We have spawned enough enemies

            if (spawningEnemies)
            {
                spawnTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;

                if (spawnTimer > 2)
                    AddEnemy(); // Time to add a new enemey
            }

            for (int i = 0; i < enemies.Count; i++)
            {
                Enemy enemy = enemies[i];
                List<Vector3> path = pathfinder.FindPath(enemy.MapPosition, new Point(9, 9), enemy.Inteligence);
                if (path != null && path.Count > 0)
                {
                    enemy.Target = path[0];
                    enemy.Update(gameTime);
                }
                else
                {
                    enemy.Position = new Vector3(0.5f, 0.0f, 0.5f);
                }

                if (enemy.IsDead)
                {
                    if (enemy.CurrentHealth > 0) // Enemy is at the end
                    {
                        enemyAtEnd = true;
                    }

                    enemies.Remove(enemy);
                    i--;
                }
            }
        }

        public void Draw(GraphicsDevice graphicsDevice, Matrix view, Matrix projection)
        {
            foreach (Enemy enemy in enemies)
                enemy.Model.render(graphicsDevice,
                   Matrix.CreateRotationX((3.1415f / 2.0f) * 3.0f) * enemy.Rotation * Matrix.CreateRotationY(MathHelper.ToRadians(-90)) *
                   Matrix.CreateScale(0.01f) *
                    Matrix.CreateTranslation(0, 0.3f, 0) * Matrix.CreateTranslation(enemy.Position),
                   view, projection);
        }
    }
}
